/**
 * 
 */
package IC.Analyzer;

import java.util.Stack;

import IC.AST.ASTNode;
import IC.AST.ASTPrinter;
import IC.Analyzer.SymTable;

/**
 *
 */
public class ASTSymTabPrinter extends ASTPrinter {

	private Stack<SymTable> prev;

	public ASTSymTabPrinter(ASTNode symbol) {
		super(symbol);
		prev = new Stack<SymTable>();
		prev.push(null);
		depth=0;
	}

	protected void enter(String header, ASTNode node) {
		System.out.printf(" %3d:",node.getLine(),node.getColumn());
		System.out.println(SPACES.substring(0,depth*ident)+header);
		SymTable symtab = node.getSymTab();

		if (symtab != prev.peek()) {
			String symtabStr = symtab.toString();
			String[] splitSymtabStr = symtabStr.split("\n");
			for(String s : splitSymtabStr) {
				System.out.println(
						SPACES.substring(0,(depth+1)*ident+1) + 
						s );
			}
		}
		depth ++;
		prev.push(symtab);
	}

	protected void enterLeave(String header, ASTNode node) {
		System.out.printf(" %3d:",node.getLine(),node.getColumn());
		SymTable symtab = node.getSymTab();
		if (symtab != prev.peek()) {
			System.out.println(symtab);
		}
		System.out.println(ASTPrinter.SPACES.substring(0,depth*ident)+header);
	}

	protected void leave() {
		depth --;
		prev.pop();
	}



}
